package com.xzzz.irda.guardcat.core.metric;


import com.xzzz.irda.guardcat.core.metric.bufferpool.BufferPoolInfo;
import com.xzzz.irda.guardcat.core.metric.classloading.ClassLoadingInfo;
import com.xzzz.irda.guardcat.core.metric.gc.GarbageCollectorInfo;
import com.xzzz.irda.guardcat.core.metric.threadpool.CustomThreadPoolInfo;
import com.xzzz.irda.guardcat.core.metric.tomcat.*;
import com.xzzz.irda.guardcat.core.metric.compiler.CompilerInfo;
import com.xzzz.irda.guardcat.core.metric.druid.DruidConnectionPoolInfo;
import com.xzzz.irda.guardcat.core.metric.druid.DruidInfo;
import com.xzzz.irda.guardcat.core.metric.dubbo.DubboThreadPoolInfo;
import com.xzzz.irda.guardcat.core.metric.jvm.JvmInfo;
import com.xzzz.irda.guardcat.core.metric.memory.BaseMemoryUsageInfo;
import com.xzzz.irda.guardcat.core.metric.memory.JvmMemoryInfo;
import com.xzzz.irda.guardcat.core.metric.memory.MemoryInfo;
import com.xzzz.irda.guardcat.core.metric.memory.MemoryUsageInfo;
import com.xzzz.irda.guardcat.core.metric.system.OperatingSystemInfo;
import com.xzzz.irda.guardcat.core.metric.thread.JvmThreadInfo;
import com.xzzz.irda.guardcat.core.metric.thread.ThreadMsg;
import com.xzzz.irda.guardcat.core.metric.tomcat.*;

import java.util.List;
import java.util.Set;

/**
 * JVM 监控工具
 *
 * @author xzzz
 * @since 1.0.0
 */
public class JvmUtil {

    /**
     * 获取操作系统信息
     *
     * @return OperatingSystemInfo
     */
    public static OperatingSystemInfo getOperatingSystemInfo() {
        return OperatingSystemInfo.getOperatingSystemInfo();
    }

    /**
     * 获取类加载信息
     *
     * @return ClassLoadingInfo
     */
    public static ClassLoadingInfo getClassLoadingInfo() {
        return ClassLoadingInfo.getClassLoadingInfo();
    }

    /**
     * 获取编译器信息
     *
     * @return CompilerInfo 编译器信息
     */
    public static CompilerInfo getCompilerInfo() {
        return CompilerInfo.getCompilerInfo();
    }

    /**
     * 获取 JVM 信息
     *
     * @return JvmInfo JVM 信息
     */
    public static JvmInfo getJvmInfo() {
        return JvmInfo.getJvmInfo();
    }

    /**
     * 获取 JVM 进程 Id
     *
     * @return String 进程 Id
     */
    public static String getJvmProcessId() {
        return JvmInfo.getJvmProcessId();
    }

    /**
     * 获取 JVM 堆内存中对象统计信息
     * 等价于使用命令行：jmap -histo pid
     *
     * @param pid 进程 ID;
     * @return String 统计信息
     */
    public static String getHeapMemoryObjectStatistics(String pid) {
        return JvmInfo.getHeapMemoryObjectStatistics(pid);
    }

    /**
     * 通过 Jcmd 执行命令
     *
     * @param command 命令
     * @return String 执行结果
     */
    public static String executeCommandByJcmd(String command) {
        return JvmInfo.executeCommandByJcmd(command);
    }

    /**
     * 获取简单的线程信息；
     *
     * @return JvmThreadInfo Jvm 线程信息
     */
    public static JvmThreadInfo getSimpleJvmThreadInfo() {
        return JvmThreadInfo.getSimpleJvmThreadInfo();
    }

    /**
     * 获取所有线程堆栈信息
     * 等价于使用命令行：jstack pid
     *
     * @param pid 进程Id
     * @return String 线程堆栈信息
     */
    public static String getFullThreadDumpMsg(String pid) {
        return JvmInfo.getFullThreadDumpMsg(pid);
    }

    /**
     * 根据线程 Id 获取线程的信息
     *
     * @param threadId 线程Id
     * @return ThreadMsg 线程信息
     */
    public static ThreadMsg getThreadMsgByThreadId(long threadId) {
        return JvmThreadInfo.getThreadMsgByThreadId(threadId);
    }

    /**
     * 获取所有的线程信息
     *
     * @return Set<ThreadMsg>
     */
    public static Set<ThreadMsg> getAllThreadMsgSet() {
        return JvmThreadInfo.getAllThreadMsgSet();
    }

    /**
     * 获取所有的死锁线程信息【包含 synchronized 死锁 和 通过 AbstractQueuedSynchronizer 实现的锁的死锁】
     *
     * @return Set<ThreadMsg>
     */
    public static Set<ThreadMsg> getDeadlockedThreadMsgSet() {
        return JvmThreadInfo.getDeadlockedThreadMsgSet();
    }

    /**
     * 获取所有的 synchronized 死锁线程信息
     *
     * @return Set<ThreadMsg>
     */
    public static Set<ThreadMsg> getMonitorDeadlockedThreadMsgSet() {
        return JvmThreadInfo.getMonitorDeadlockedThreadMsgSet();
    }

    /**
     * 获取 JVM 内存信息
     *
     * @return JvmMemoryInfo  JVM 内存信息
     */
    public static JvmMemoryInfo getJvmMemoryInfo() {
        return JvmMemoryInfo.getJvmMemoryInfo();
    }

    /**
     * 获取 JVM 堆内存使用信息
     *
     * @return BaseMemoryUsageInfo 基础内存使用信息
     */
    public static BaseMemoryUsageInfo getHeapMemoryUsageInfo() {
        return MemoryInfo.getHeapMemoryUsageInfo();
    }

    /**
     * 获取 JVM 非堆内存使用信息
     *
     * @return BaseMemoryUsageInfo 基础内存使用信息
     */
    public static BaseMemoryUsageInfo getNonHeapMemoryUsageInfo() {
        return MemoryInfo.getNonHeapMemoryUsageInfo();
    }

    /**
     * 获取 JVM 中 JIT 编译代码缓存区内存内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getCodeCacheMemoryUsageInfo() {
        return MemoryInfo.getCodeCacheMemoryUsageInfo();
    }

    /**
     * 获取 Jvm 元空间内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getMetaspaceMemoryUsageInfo() {
        return MemoryInfo.getMetaspaceMemoryUsageInfo();
    }

    /**
     * 获取 JVM 类压缩数据区内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getCompressedClassSpaceMemoryUsageInfo() {
        return MemoryInfo.getCompressedClassSpaceMemoryUsageInfo();
    }

    /**
     * 获取 JVM 新生代内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getEdenSpaceMemoryUsageInfo() {
        return MemoryInfo.getEdenSpaceMemoryUsageInfo();
    }

    /**
     * 获取 JVM 幸存代内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getSurvivorSpaceMemoryUsageInfo() {
        return MemoryInfo.getSurvivorSpaceMemoryUsageInfo();
    }

    /**
     * 获取 JVM 老年代内存使用信息
     *
     * @return MemoryUsageInfo  内存使用信息
     */
    public static MemoryUsageInfo getOldGenMemoryUsageInfo() {
        return MemoryInfo.getOldGenMemoryUsageInfo();
    }

    /**
     * 获取 JVM 所有的内存信息
     *
     * @return MemoryInfo 内存信息
     */
    public static MemoryInfo getMemoryInfo() {
        return MemoryInfo.getMemoryInfo();
    }

    /**
     * 获取 JVM 缓冲池信息【nio 中的直接内存信息】
     *
     * @return BufferPoolInfo 缓冲池信息
     */
    public static BufferPoolInfo getBufferPoolInfo() {
        return BufferPoolInfo.getBufferPoolInfo();
    }

    /**
     * 获取 JVM 所有垃圾收集器信息【触发 GC 之后才会有信息】
     *
     * @return List<GarbageCollectorInfo>
     */
    public static List<GarbageCollectorInfo> getGarbageCollectorInfos() {
        return GarbageCollectorInfo.getGarbageCollectorInfos();
    }

    /**
     * 获取 Tomcat 服务器信息
     *
     * @return List<TomcatServerInfo>
     */
    public static List<TomcatServerInfo> getTomcatServerInfos() {
        return TomcatInfo.getTomcatServerInfos();
    }

    /**
     * 获取 Tomcat 的全局请求处理器的监控信息
     *
     * @return List<TomcatGlobalRequestProcessorInfo>
     */
    public static List<TomcatGlobalRequestProcessorInfo> getTomcatGlobalRequestProcessorInfos() {
        return TomcatInfo.getTomcatGlobalRequestProcessorInfos();
    }

    /**
     * 获取 Tomcat 连接器的监控信息
     *
     * @return List<TomcatConnectorInfo>
     */
    public static List<TomcatConnectorInfo> getTomcatConnectorInfos() {
        return TomcatInfo.getTomcatConnectorInfos();
    }

    /**
     * 获取 Tomcat 协议处理器的监控信息
     *
     * @return List<TomcatProtocolHandlerInfo>
     */
    public static List<TomcatProtocolHandlerInfo> getTomcatProtocolHandlerInfos() {
        return TomcatInfo.getTomcatProtocolHandlerInfos();
    }

    /**
     * 获取 Tomcat 的线程池的监控信息
     *
     * @return List<TomcatThreadPoolInfo>
     */
    public static List<TomcatThreadPoolInfo> getTomcatThreadPoolInfos() {
        return TomcatInfo.getTomcatThreadPoolInfos();
    }

    /**
     * 获取 Tomcat 中 DispatcherServletInfo 的监控
     *
     * @return List<TomcatDispatcherServletInfo>
     */
    public static List<TomcatDispatcherServletInfo> getTomcatDispatcherServletInfos() {
        return TomcatInfo.getTomcatDispatcherServletInfos();
    }

    /**
     * 获取自定义线程池的监控信息
     *
     * @return List<CustomThreadPoolInfo>
     */
    public static List<CustomThreadPoolInfo> getCustomThreadPoolInfos() {
        return CustomThreadPoolInfo.getCustomThreadPoolInfos();
    }

    /**
     * 获取 apache dubbo 线程池监控
     * <p>
     * return List<DubboThreadPoolInfo>
     */
    public static List<DubboThreadPoolInfo> getApacheDubboThreadPoolInfos() {
        return DubboThreadPoolInfo.getApacheDubboThreadPoolInfos();
    }

    /**
     * 获取 druid 连接池信息
     *
     * @return DruidConnectionPoolInfo
     */
    public static DruidConnectionPoolInfo getDruidConnectionPoolInfo() {
        return DruidInfo.getDruidConnectionPoolInfo();
    }

}
